﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using TIMETABLEASY.EasyCalendar.Model;

namespace TIMETABLEASY.EasyCalendar.Controler
{
    public class CalendarDay : ItemsControl
    {
        static CalendarDay()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CalendarDay), new FrameworkPropertyMetadata(typeof(CalendarDay)));
        }

        #region DayIndex

        public static readonly DependencyProperty DayIndexProperty =
            DependencyProperty.Register("DayIndex", typeof(int), typeof(CalendarDay),
                new FrameworkPropertyMetadata(0));

        public int DayIndex
        {
            get { return (int)GetValue(DayIndexProperty); }
            set { SetValue(DayIndexProperty, value); }
        }

        #endregion

        #region CurrentDate

        public static readonly DependencyProperty CurrentDateProperty =
            DependencyProperty.Register("CurrentDate", typeof(DateTime), typeof(TimeTable),
                new FrameworkPropertyMetadata((DateTime)DateTime.Now, onCurrentDateChanged));
            
                

        public DateTime CurrentDate
        {
            get { return (DateTime)GetValue(CurrentDateProperty); }
            set { SetValue(CurrentDateProperty, value); }
        }

        protected static void onCurrentDateChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            ((CalendarDay)obj).onCurrentDateChanged(args);
        }

        protected virtual void onCurrentDateChanged(DependencyPropertyChangedEventArgs e)
        {
            TextBlock dayHeader = this.GetTemplateChild("dayHeader") as TextBlock;
            //dayHeader.Text = CurrentDate.DayOfWeek.ToString();
            dayHeader.Text = "toto";
        }

        private static int getIntDay(String day)
        {
            
            int val;
            switch (day)
            {
                case "Monday":
                    val = 1;
                    break;

                case "Tuesday":
                    val = 2;
                    break;

                case "Wednesday":
                    val = 3;
                    break;

                case "Thursday":
                    val = 4;
                    break;

                case "Friday":
                    val = 5;
                    break;

                case "Saturday":
                    val = 6;
                    break;

                default:
                    val = 7;
                    break;
            }

            return val;
        }

        public void nextWeek()
        {
            initDates();
            jump_2_x_week(-1);
        }

        public void previousWeek()
        {
            initDates();
            jump_2_x_week(1);
        }

        public void jump_2_x_week(int nb_week)
        {
            CurrentDate -= TimeSpan.FromDays(7 * nb_week);
            TextBlock dayHeader = this.GetTemplateChild("dayHeader") as TextBlock;
            dayHeader.Text = CurrentDate.DayOfWeek.ToString() + " " + CurrentDate.Day.ToString() + "th " + getMounth(CurrentDate.Month);

            this.ItemsSource = Events.ByDate(CurrentDate);
        }

        public void initDates()
        {
            int todayInt = getIntDay(CurrentDate.DayOfWeek.ToString());
            int dayInt = getIntDay(this.Name);
            int interval = todayInt - dayInt;

            if (interval>=0)
            {
                CurrentDate = CurrentDate - TimeSpan.FromDays(interval);
                CurrentDate = Convert.ToDateTime(CurrentDate.Day.ToString() + "/" + CurrentDate.Month.ToString() + "/" + CurrentDate.Year.ToString() + " 00:00:00");
            } else {
                interval = 0 - interval;
                CurrentDate = CurrentDate + TimeSpan.FromDays(interval);
                CurrentDate = Convert.ToDateTime(CurrentDate.Day.ToString() + "/" + CurrentDate.Month.ToString() + "/" + CurrentDate.Year.ToString() + " 00:00:00");
            }

            TextBlock dayHeader = this.GetTemplateChild("dayHeader") as TextBlock;
            dayHeader.Text = CurrentDate.DayOfWeek.ToString() + " " + CurrentDate.Day.ToString() + "th " + getMounth(CurrentDate.Month);

            this.ItemsSource = Events.ByDate(CurrentDate);
        }

        public String getMounth(int mounth)
        {
            String mounth_str;
            switch (mounth)
            {
                case 1:
                    mounth_str = "January";
                    break;

                case 2:
                    mounth_str = "February";
                    break;

                case 3:
                    mounth_str = "March";
                    break;

                case 4:
                    mounth_str = "April";
                    break;

                case 5:
                    mounth_str = "May";
                    break;

                case 6:
                    mounth_str = "June";
                    break;

                case 7:
                    mounth_str = "July";
                    break;

                case 8:
                    mounth_str = "August";
                    break;

                case 9:
                    mounth_str = "September";
                    break;

                case 10:
                    mounth_str = "October";
                    break;

                case 11:
                    mounth_str = "November";
                    break;

                default:
                    mounth_str = "December";
                    break;
            }

            return mounth_str;
        }

        #endregion

        #region Events

        public static readonly DependencyProperty EventsProperty =
            DependencyProperty.Register("Events", typeof(IEnumerable<EventCal>), typeof(CalendarDay),
            new FrameworkPropertyMetadata(null, new PropertyChangedCallback(CalendarDay.OnEventsChanged)));

        public IEnumerable<EventCal> Events
        {
            get { return (IEnumerable<EventCal>)GetValue(EventsProperty); }
            set { SetValue(EventsProperty, value); }
        }

        private static void OnEventsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((CalendarDay)d).OnEventsChanged(e);
        }

        protected virtual void OnEventsChanged(DependencyPropertyChangedEventArgs e)
        {
            FilterAppointments();
        }

        private void FilterAppointments()
        {
            DateTime byDate = CurrentDate;
            this.ItemsSource = Events.ByDate(byDate);
        }

        #endregion 

        #region ItemsControl Container Override

        protected override DependencyObject GetContainerForItemOverride()
        {
            return new EventCalendar();
        }

        protected override bool IsItemItsOwnContainerOverride(object item)
        {
            return (item is EventCalendar);
        }

        #endregion
    }
}
